import Vue from "vue";
import ContextApi from "@/api/ContextApi";
import AxiosApi from "@/api/AxiosApi";
import api from '../static/api'
import GlobalAxiosConfig from "./DynamicAPIProvider/GlobalAxiosConfig";
import {
  showFullScreenLoading,
  tryHideFullScreenLoading,
} from "@/utils/loading";
import md5 from "js-md5";
import Cookie from "js-cookie";
Vue.prototype.Cookie = Cookie;
Vue.prototype.$md5 = md5;

function onRequest(axiosConfig) {
  showFullScreenLoading();
}

function onResponse(res, ctx) {
  tryHideFullScreenLoading();
}

function onError(err) {
  tryHideFullScreenLoading();
}

let params = [
  // {
  //   baseURL: "/webapi",
  // },
  onRequest,
  onResponse,
  onError,
];

const apiPlugin = (context) => {
  context.$axios.interceptors.response.use(
    function (response) {
      return response;
    },
    function (error) {
      return Promise.resolve(undefined);
    }
  );

  context.$api = GlobalAxiosConfig(
    ContextApi,
    context.$axios,
    context,
    {
      baseURL: api.apiOther,
    },
    ...params
  );
  Vue.prototype.$api = GlobalAxiosConfig(
    AxiosApi,
    context.$axios,
    context,
    {
      baseURL: "/webapi",
    },
    ...params
  );
};

export default apiPlugin;
